package com.codetile.batch.file;

import com.codetile.batch.db.User;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.file.FlatFileItemReader;
import org.springframework.batch.item.file.MultiResourceItemReader;
import org.springframework.batch.item.file.mapping.DefaultLineMapper;
import org.springframework.batch.item.file.mapping.FieldSetMapper;
import org.springframework.batch.item.file.transform.DelimitedLineTokenizer;
import org.springframework.batch.item.file.transform.FieldSet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.validation.BindException;

@EnableBatchProcessing
@Configuration
public class MultiFIleReaderDemo {
    @Autowired
    private JobBuilderFactory jobBuilderFactory;

    @Autowired
    private StepBuilderFactory stepBuilderFactory;

    @Autowired
    @Qualifier("dbJdbcWrite")
    private ItemWriter<? super User> dbWrite;

    @Value("classpath:/user*.txt")
    private Resource[] resources;

    @Bean
    public Job multiJob(){
        return jobBuilderFactory.get("multiJob")
                .start(multiStep())
                .build();
    }

    @Bean
    public Step multiStep(){
        return stepBuilderFactory.get("multiStep")
                .<User,User>chunk(2)
                .reader(multiReader())
                .writer(dbWrite)
                .build();
    }

    @Bean
    @Scope
    public MultiResourceItemReader<User> multiReader(){

        MultiResourceItemReader<User> reader=new MultiResourceItemReader<>();
        reader.setDelegate(file1Reader());
        reader.setResources(resources);
        return reader;
    }


    @Bean
    @StepScope
    public FlatFileItemReader<User> file1Reader(){
        FlatFileItemReader<User> reader=new FlatFileItemReader<>();
        reader.setResource(new ClassPathResource("user.txt"));
        //跳过第一行
        reader.setLinesToSkip(1);
        DelimitedLineTokenizer tokenizer=new DelimitedLineTokenizer();
        tokenizer.setNames(new String[]{"id","name","age"});
        DefaultLineMapper<User> mapper=new DefaultLineMapper<>();
        mapper.setLineTokenizer(tokenizer);
        mapper.setFieldSetMapper(new FieldSetMapper<User>() {
            @Override
            public User mapFieldSet(FieldSet fieldSet) throws BindException {
                User user=new User();
                user.setId(fieldSet.readInt("id"));
                user.setName(fieldSet.readString("name"));
                user.setAge(fieldSet.readInt("age"));
                return user;
            }
        });

        mapper.afterPropertiesSet();
        reader.setLineMapper(mapper);
        return reader;
    }

}
